package com.whmskj.xjlhsc.qianYiData.job;

import com.whmskj.xjlhsc.qianYiData.service.DailyPowerSupplyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDate;

@Slf4j
@Component
public class DailyPowerSupplyJob {

    @Resource
    private DailyPowerSupplyService dailyPowerSupplyService;

    /**
     * 每3分钟同步一次当日每日电量汇总至 itsADailyPowerSupply
     * 保证当天数据随采集更新
     */
    @Scheduled(cron = "0 */2 * * * ?")
    public void syncToday() {
        try {
            LocalDate day = LocalDate.now();
            int affected = dailyPowerSupplyService.syncDay(day);
            log.info("DailyPowerSupplyJob[today]: synced day {} into itsADailyPowerSupply, affected {} rows", day, affected);
        } catch (Exception e) {
            log.error("DailyPowerSupplyJob[today]: sync failed", e);
        }
    }

    /**
     * 每天00:05回填昨日数据，确保每天都有一条稳定的日汇总
     */
    @Scheduled(cron = "0 10 0 * * ?")
    public void syncYesterday() {
        try {
            LocalDate day = LocalDate.now().minusDays(1);
            int affected = dailyPowerSupplyService.syncDay(day);
            log.info("DailyPowerSupplyJob[yesterday]: synced day {} into itsADailyPowerSupply, affected {} rows", day, affected);
        } catch (Exception e) {
            log.error("DailyPowerSupplyJob[yesterday]: sync failed", e);
        }
    }
}
